package cn.controlsys.common.config;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Author: shanzheng <br/>
 * Description: RabbitMQ配置文件<br/>
 * Date： 14:11 2018/1/26.<br/>
 * Modified By: <br/>
 */
@Configuration
public class RabbitMQConfig {
    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    @Value("${spring.rabbitmq.port}")
    private Integer port;

    @Value("${spring.rabbitmq.virtualHost}")
    private String virtualHost;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        /*
         * 出现connection closed错误 可以选择增加连接数,
         * 当然也有别的选择办法：http://blog.csdn.net/u013851082/article/details/69223393
         */
//        connectionFactory.setChannelCacheSize(3);

        /* 这里需要显示调用,才能进行消息的回调 */
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }
    //并发消费
//    @Bean("pointJobContainerFactoryConfirm")
//    public SimpleRabbitListenerContainerFactory pointTaskContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setPrefetchCount(1);//每个消费者获取最大投递数量 默认6
//        factory.setConcurrentConsumers(1);//消费者数量，默认3
//        configurer.configure(factory, connectionFactory());
//        return factory;
//    }

}
